﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ABC;
using System.Text;
using System.Collections.Generic;
using ABC.Google;
using ABC.Tech;

namespace GoogleMap
{
    // A sample project by Ghaffar khan

    public partial class Map : System.Web.UI.Page
    {
        private System.DateTime startDate;
        private System.DateTime endDate;
        private Boolean showTrips;
        private Boolean showWOOnsitePartsReceived;
        private Boolean showUnitRequiringMoreInfo;
        private Boolean showPartsIn7OrLessDays;
        private List<ABC.Google.PartsIn7orLessDays> ListOfParts7OrLessDays;
        private ABC.Google.MapMarkerCollection<ABC.Google.UnitsRequiringMoreInfo> ListOfUnitsNeedingMoreInfo;
        private ABC.Google.MapMarkerCollection<ABC.Google.WOONsitePartsReceived> ListOfOnSitePartsReceived;
        private ABC.Google.MapMarkerCollection<ABC.Google.currentTrips> currTrips;

        protected void Page_Load(object sender, EventArgs e)
        {   
            if (txtStartDate.Text != "" && txtEndDate.Text != "")
            {
                startDate = Convert.ToDateTime(txtStartDate.Text);
                endDate = Convert.ToDateTime(txtEndDate.Text);
                currTrips = ABC.Trips.Trips.getListOfTrips(startDate, endDate, DropDownTechs.SelectedValue,null);
                currTrips.ToString();
                showTrips = this.chkboxTrips.Checked;
            }
         
            showWOOnsitePartsReceived = this.chkBoxWOONsitePartsReceived.Checked;
            showUnitRequiringMoreInfo = this.chkboxUnitsRequiringMoreInfo.Checked;
            showPartsIn7OrLessDays = this.chkboxpartsIn7OrLessDays.Checked;

            if (showPartsIn7OrLessDays)
                //ListOfParts7OrLessDays = ABC.WorkOrders.WorkOrder.getPartsIn7orLessDays();
            if (showUnitRequiringMoreInfo)
              //  ListOfUnitsNeedingMoreInfo = ABC.WorkOrders.WorkOrder.getUnitsRequiringMoreInfo();
            if (showWOOnsitePartsReceived)
              //  ListOfOnSitePartsReceived = ABC.WorkOrders.WorkOrder.getWOOnsitePartsReceived();

            DropDownTechs.DataSource = ABC.Tech.Tech.getListOfTechInitials();
            DropDownTechs.DataBind();
            BuildScript();
        }

        /// <summary>
        /// Creates the Java Script Code
        /// </summary>
        private void BuildScript()
        {
            
            // construct the final script
            StringBuilder sb = new StringBuilder();

            sb.Append(@"        
            <script type='text/javascript'>

            window.onload = function() {
             // Creating a MapOtions object with the required properties
                var options = {
                    zoom: 10,
                    center: new google.maps.LatLng(43.1158400,-77.4806270),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                             };

               // Creating the map
               var map = new google.maps.Map(document.getElementById('map_canvas'), options);
                
             
                
                ");

            if (showWOOnsitePartsReceived == true)
            {
                sb.Append("   displayMarkers(map, woOnSitePartsReceived,'icons/yellow01.png');");
            }
            if (showUnitRequiringMoreInfo == true)
            {
                sb.Append("   displayMarkers(map, unitsRequiringMoreInfo,'icons/green01.png');");
            }
            if (showPartsIn7OrLessDays == true)
            {
                sb.Append("   displayMarkers(map, partsIn7OrLessDays,'icons/purple01.png');");
            }
            if (showTrips)
            {
                sb.Append("   displayScheduledTrips(map,scheduledTrips);");
            }
            sb.Append("    }; ");

            #region populate/set array
            if (showTrips == true)
            {
                sb.Append("var  scheduledTrips=[ ");
                foreach (ABC.Google.currentTrips x in currTrips)
                {
                    sb.Append("['" + x.invoice + "', " + x.Latitude + "," + x.Longitude + ",'" + x.imagePath + "','" + x.tripDate + "','" + x.tech  + "'],");
                }

                sb.Append("];");
            }

            if (showWOOnsitePartsReceived == true)
                {
                    sb.Append("var  woOnSitePartsReceived=[ ");
                    foreach (ABC.Google.WOONsitePartsReceived x in ListOfOnSitePartsReceived)
                    {
                        sb.Append("['" + x.invoice + "'," + x.Latitude + "," + x.Longitude + ",'" + x.ContentDescription +"'],");
                    }
                       sb.Append("];");

                }
                if (showUnitRequiringMoreInfo == true)
                {
                    sb.Append("var  unitsRequiringMoreInfo =[ ");
                    //add data here
                    foreach (ABC.Google.UnitsRequiringMoreInfo x in ListOfUnitsNeedingMoreInfo)
                    {
                        sb.Append("['" + x.invoice + "'," + x.Latitude + "," + x.Longitude + ",'" + x.ContentDescription + "'],");
                    }

                    sb.Append("];");
                }

                if (showPartsIn7OrLessDays == true)
                {
                    sb.Append("var  partsIn7OrLessDays =[ ");
                    //add data here
                    foreach (ABC.Google.PartsIn7orLessDays x in ListOfParts7OrLessDays)
                    {
                        sb.Append("['" + x.invoice + "'," + x.Latitude + "," + x.Longitude + ",'" + x.ContentDescription + "'],");
                    }

                    sb.Append("];");
                }
            #endregion

                sb.Append(@" function displayMarkers(map, locations,path)
                             {
                                //var image = new google.maps.MarkerImage(path);
                                var image = new google.maps.MarkerImage(path);
                                for (var i = 0; i < locations.length; i++) {
                                    var beach = locations[i];
                                    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);

                                    var contentString = beach[3];

                                    var marker = new google.maps.Marker({
                                        position: myLatLng,
                                        map: map,
                                        icon: image,
                                        title: 'WO: ' + beach[0]
                                    });

                                        (function(tripInfo,marker) {
                                            google.maps.event.addListener(marker, 'click',function(){
                                              if(!infowindow) {
                                                infowindow = new google.maps.InfoWindow();
                                              }
                                              infowindow.setContent(tripInfo);
                                              infowindow.open(map,marker);
                                            });
                                          })(contentString,marker);

                                  }// end of loop
                             }// end of setMarkers function
                ");

                sb.Append(@"
                              //variable to hold the infowindow object
                              var infowindow;
                              function displayScheduledTrips(map, locations)
                             {
                                                             
                                for (var i = 0; i < locations.length; i++) {                          
                                    var beach = locations[i];
                                    var image = new google.maps.MarkerImage(beach[3]);
                                    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);

                                    var contentString = '<p><b><h3>WO: '+ beach[0] +'</h3><h4> ' + beach[4] +'</h4><h4>Tech: ' + beach[5] +'</h4></p>';
                                     
                                    var marker = new google.maps.Marker({
                                        position: myLatLng,
                                        map: map,
                                        icon: image,
                                        title: 'WO: ' + beach[0]                                   
                                    }); // end of marker
                                   
                                (function(tripInfo,marker) {
                                google.maps.event.addListener(marker, 'click',function(){
                                  if(!infowindow) {
                                    infowindow = new google.maps.InfoWindow();
                                  }
                                  infowindow.setContent(tripInfo);
                                  infowindow.open(map,marker);
                                });
                               })(contentString,marker);

                                  }//end of for loop

                             }// end of setMarkers function
                ");
                sb.Append(" </script>");
                js.Text = sb.ToString();
            }

        }
    
}
